﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._sujiahao;
using _2112A_2_MES.WangWeiHaos.Inputs;
using _2112A_2_MES.WangWeiHaos.IService;
using _2112A_2_MES.WangWeiHaos.Outputs;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.WangWeiHao.Service
{
    [ApiExplorerSettings(GroupName = "王炜豪")]
    public class WorkShopService : ApplicationService, IWorkShopService
    {
        public readonly IRepository<MdWorkshop,int> _workshopRepository;
        public WorkShopService(IRepository<MdWorkshop,int> workshopRepository)
        {
            _workshopRepository = workshopRepository;
        }

        /// <summary>
        /// 新增车间
        /// </summary>
        /// <param name="workshop">车间实体</param>
        /// <returns>返回受影响的行数</returns>
        [HttpGet, Route("/api/WorkShop/AddWorkShop")]
        public async Task<int> AddWorkShop(MdWorkshop workshop)
        {
            workshop.CreateBy = "wangweihao";
            workshop.CreateTime = DateTime.Now;
            await _workshopRepository.InsertAsync(workshop);
            return await Task.FromResult(1);
        }

        /// <summary>
        /// 删除车间
        /// </summary>
        /// <param name="id">车间id</param>
        /// <returns></returns>
        [HttpGet, Route("/api/WorkShop/DeleteWorkShop")]
        public async Task<int> DeleteWorkShop(int id)
        {
            await _workshopRepository.DeleteAsync(id);
            return await Task.FromResult(1);
        }

        /// <summary>
        /// 获取车间信息
        /// </summary>
        /// <param name="id">车间id</param>
        /// <returns></returns>
        [HttpGet, Route("/api/WorkShop/GetWorkShopById")]
        public async Task<MdWorkshop> GetWorkShopById(int id)
        {
            var workshop = await _workshopRepository.GetAsync(id);
            return workshop;
        }

        /// <summary>
        /// 获取车间列表
        /// </summary>
        /// <param name="workshopCode">车间编号</param>
        /// <param name="workshopName">车间名称</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        [HttpGet, Route("/api/WorkShop/GetWorkShopList")]
        public async Task<ReturnSelect<object>> GetWorkShopList(string? workshopCode, string? workshopName, int pageIndex, int pageSize)
        {
            var workshopList = await _workshopRepository.GetListAsync();
            if (!string.IsNullOrEmpty(workshopCode))
            {
                workshopList = workshopList.Where(x => x.WorkshopCode.Contains(workshopCode)).ToList();
            }
            if (!string.IsNullOrEmpty(workshopName))
            {
                workshopList = workshopList.Where(x => x.WorkshopName.Contains(workshopName)).ToList();
            }
            var totalCount = workshopList.Count;
            var pageList = workshopList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var workshopPageList = AutoMapperExt.MapToList<MdWorkshop, WorkShopOutput>(pageList);
            var data = new { TotalCount = totalCount, PageList = workshopPageList };
            if (workshopList != null)
            {
                return new ReturnSelect<object>(true, "0", data);
            }
            else
            {
                return new ReturnSelect<object>(true, "0", null);
            }
        }

        /// <summary>
        /// 修改车间信息
        /// </summary>
        /// <param name="workshop">车间dto</param>
        /// <returns></returns>
        [HttpGet, Route("/api/WorkShop/UpdateWorkShop")]
        public async Task<int> UpdateWorkShop(WorkshopInput workshop)
        {
            var workshopEntity = await _workshopRepository.GetAsync(workshop.id);
            workshopEntity.WorkshopCode = workshop.WorkshopCode;
            workshopEntity.WorkshopName = workshop.WorkshopName;
            workshopEntity.Area = workshop.Area;
            workshopEntity.Charger = workshop.Charger;
            workshopEntity.EnableFlag = workshop.EnableFlag;
            workshopEntity.UpdateBy = "wangweihao";
            workshopEntity.UpdateTime = DateTime.Now;
            await _workshopRepository.UpdateAsync(workshopEntity);
            return await Task.FromResult(1);
        }
    }
}
